<template>
  <div class="login-container">
    <div class="login-form">
      <div class="input-group">
        <i class="icon user-icon"></i>
        <input type="text" v-model="loginForm.username" placeholder="用户名" />
      </div>
      <div class="input-group">
        <i class="icon lock-icon"></i>
        <input type="password" v-model="loginForm.password" placeholder="密码" />
      </div>

      <button class="login-btn" @click="handleLogin" :disabled="loading">
        <span v-if="!loading">登录</span>
        <span v-else class="loading-spinner"></span>
      </button>
    </div>
  </div>
</template>

<script>
import axios from 'axios';
const { ipcRenderer } = window.require('electron');
import { serviceCAD, serviceExe, serviceFront } from '../../api/axios';

export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loading: false,
      username: '',
      password: '',
      loginUrl: ''
    }
  },
  mounted() {
    const parseUrl = decodeURIComponent(window.location.href);
    const queryParams = this.getQueryParams(parseUrl);
    if (queryParams && queryParams.exe) {
      this.username = queryParams.username;
      this.password = queryParams.password;
      this.loginUrl = queryParams.loginUrl;
    }
  },
  methods: {
    getQueryParams(url) {
      let params = {};
      let queryString = url.split('?')[1];
      if (!queryString) return params;
      queryString.split('&').forEach(param => {
        let [key, value] = param.split('=');
        params[key] = decodeURIComponent(value);
      });
      return params;
    },
    async handleLogin() {
      if (this.loading) return; // 防止重复点击
      this.loading = true;
      let username = this.username || this.loginForm.username;
      let password = this.password || this.loginForm.password;
      let loginApiUrl = this.loginUrl;
      try {
        const params = { username, password };
        const response = await axios.post(loginApiUrl, params);
        if (response.data.code == 200) {
          const token = response.data.data.token;
          serviceCAD.defaults.headers.common['Authorization'] = token || '';
          serviceExe.defaults.headers.common['Authorization'] = token || '';
          serviceFront.defaults.headers.common['Authorization'] = token || '';
          ipcRenderer.invoke('set-token', token);
          ipcRenderer.send('store-set', 'token', token);
          ipcRenderer.send('store-set', 'username', username);
          this.$router.push("/visual");
        } else {
          this.$message.error(response.data.message || '登录失败');
        }
      } catch (error) {
        console.error('登录请求出错:', error);
        this.$message.error('服务器错误，请稍后再试');
      } finally {
        this.loading = false;
      }
    }
  }
}
</script>

<style scoped>
.login-container {
  width: 100%;
  height: 100vh;
  background: url("../../assets/login-bg.png");
  background-size: 100% 100%;
  display: flex;
  justify-content: flex-end;
  align-items: center;
}

.login-form {
  width: 200px;
  display: flex;
  flex-direction: column;
  gap: 20px;
  margin-right: 200px;
}

.input-group {
  position: relative;
  width: 100%;
  border-bottom: 1px solid #396ecc;
  padding-left: 35px;
}

.input-group:focus-within {
  border-bottom-color: #ffffff;
  /* 你可以换成想要的颜色 */
}

.input-group .icon {
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 10px;
  height: 10px;
  background-size: contain;
  background-repeat: no-repeat;
}

.user-icon {
  background-image: url("../../assets/user-icon.png");
}

.lock-icon {
  background-image: url("../../assets/lock-icon.png");
}

.input-group input {
  width: 100%;
  height: 30px;
  border: none;
  outline: none;
  background: transparent;
  font-size: 11px;
  color: #ffffff;
}

.login-btn {
  width: 200px;
  height: 26px;
  background: #396ecc;
  border-radius: 5px;
  color: #fff;
  font-size: 11px;
  border: none;
  cursor: pointer;
  margin-top: 10px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.login-btn[disabled] {
  opacity: 0.7;
  cursor: not-allowed;
}

.login-btn:hover:not([disabled]) {
  background: #2f5bb5;
}

/* 旋转loading */
.loading-spinner {
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-top: 2px solid #fff;
  border-radius: 50%;
  width: 14px;
  height: 14px;
  animation: spin 0.6s linear infinite;
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }

  100% {
    transform: rotate(360deg);
  }
}
</style>
